package com.bao.controller;

import com.bao.common.ResultMessage;
import com.bao.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bao
 * Date: 2020/05/24 21:07
 * Description:
 * Version: V1.0
 */
@Controller
public class LoginServlet {

    @RequestMapping(value = "login/{name}/{sex}",method = RequestMethod.GET)
    public void  login(@PathVariable("name") String name, @PathVariable("sex")String sex){
        System.out.println(name+sex+"姓名+性别");
    }
    @RequestMapping(value = "login2",method = RequestMethod.POST)
    public String  login2()  {

        return "forward:/WEB-INF/page/login.html";
    }
    @RequestMapping(value = "login3",method = RequestMethod.POST)
    public String  login3(Model model)  {
        Student student1=new Student();
        student1.setS_id(new Long(1));
        student1.setS_name("娜娜");
        student1.setS_sex("女");
        Student student2=new Student();
        student2.setS_id(new Long(2));
        student2.setS_name("包善州");
        student2.setS_sex("男");
        List<Student> list=new ArrayList();
        list.add(student1);
        list.add(student2);
        model.addAttribute("list",list);
        return  "test";
    }
    @RequestMapping(value = "login4",method = RequestMethod.POST)
    public ModelAndView  login4(Model model)  {
        Student student1=new Student();
        student1.setS_id(new Long(1));
        student1.setS_name("娜娜啊");
        student1.setS_sex("女");
        Student student2=new Student();
        student2.setS_id(new Long(2));
        student2.setS_name("包善州");
        student2.setS_sex("男");
        List<Student> list=new ArrayList();
        list.add(student1);
        list.add(student2);
        ModelAndView modelAndView=new ModelAndView("test");
        //modelAndView.setViewName("test");
        modelAndView.addObject("list",list);
        return  modelAndView;
    }


    @RequestMapping(value = "login5",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public List  login5()  {
        Student student1=new Student();
        student1.setS_id(new Long(1));
        student1.setS_name("娜娜");
        student1.setS_sex("女");
        Student student2=new Student();
        student2.setS_id(new Long(2));
        student2.setS_name("包善州啊 ");
        student2.setS_sex("男");
        List<Student> list=new ArrayList();
        list.add(student1);
        list.add(student2);
//        ModelAndView modelAndView=new ModelAndView("test");
//        modelAndView.addObject("list",list);
        return  list;
    }

    @RequestMapping(value = "login6",method = RequestMethod.POST,produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String  login6()  {
        return  "ys";
    }

    @RequestMapping(value = "login7",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public List  login7(String name)  {
        System.out.println("前端的数据"+name);
        Student student1=new Student();
        student1.setS_id(new Long(1));
        student1.setS_name("娜娜");
        student1.setS_sex("女");

        int i=new Integer("男");

        List<Student> list=new ArrayList();
        list.add(student1);
        return  list;
    }
    @RequestMapping(value = "login8",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public List  login8(@RequestBody String name)  {
        System.out.println("前端的数据"+name);
        Student student1=new Student();
        student1.setS_id(new Long(1));
        student1.setS_name("li");
        student1.setS_sex("女");
        List<Student> list=new ArrayList();
        list.add(student1);
        int i=1/0;
        return  list;
    }

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public ResultMessage handlerException(){
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(0);
        resultMessage.setMessage("系统出现错误，请联系管理员");
        return  resultMessage;
    }




}
